/**
 * Copyright (C) 2009 IcyRiver / Wesley He
 *
 * IcyRiver Lib may be copied only under the terms of the GNU General
 * Public License V1, which may be found in the IcyRiver.net source kit.
 * Please visit the IcyRiver Home Page http://icyriver.net/ for more detail.
 **/

//socket_client.c

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <time.h>
#include <stdlib.h>
#include <fcntl.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netdb.h>

#include "socket.h"
#include "socket_client.h"

int g_server_port = SOCKET_SERVER_DEFAULT_PORT;

int main(int argc, char *argv[])
{

	if (argc != 2)  
	{ 
		fprintf(stderr,"file: %s, line: %d: Usage: socket_client ip\n", \
				__FILE__, __LINE__);
		return 0;
	} 

	int result;
	int err_no;
	char server_ip[strlen(argv[1])];
	int sock;

	memcpy(server_ip, argv[1], strlen(argv[1]));
	fprintf(stderr,"file: %s, line: %d: Connecting to server %s .....\n", \
			__FILE__, __LINE__, server_ip);

	if ((result=g_socket_connect_server(server_ip, g_server_port, &err_no)) < 0)
	{
		return 0;
	}

	fprintf(stderr,"file: %s, line: %d: Successful! Connected to server %s\n", \
			__FILE__, __LINE__, server_ip);

	sock = result;
	if (send(sock, server_ip, 100, 0) == -1) 
	{
#ifdef __DEBUG__
		fprintf(stderr,"file: %s, line: %d: sent messages to server %s failed\n", \
				__FILE__, __LINE__, server_ip);
#endif
	}

#ifdef __DEBUG__
	fprintf(stderr,"file: %s, line: %d: Disconnecting to server %s ....\n", \
			__FILE__, __LINE__, server_ip);
#endif
	close(sock);
#ifdef __DEBUG__
	fprintf(stderr,"file: %s, line: %d: Successful! Disconnected to server %s\n", \
			__FILE__, __LINE__, server_ip);
#endif
	return 0;
}
